package Review.LinkedList;

public class MyLinkedList {
    static class  ListNode{
        public  int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }

    }
    public ListNode head;
    //打印
    public  void display(){
        ListNode cur = head;
        while (cur !=null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
    }
    //计算链表的个数
    public int size(){
        int len =0;
        ListNode cur = head;
        while (cur !=null){
            len++;
            cur = cur.next;
        }
        return len;
    }
    //查找是否包含要查找的key是否在单链表当中
    public boolean contains(int key){
        ListNode cur =head;
        while (cur !=null){
            if (cur.val==key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //头插法
    public void addFist(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }
    //尾插法
    public  void addLast(int data){
        ListNode cur =head;
        ListNode node = new ListNode(data);
        if (head==null){
            head = node;
            return;
        }
        while (cur !=null){
            cur = cur.next;
        }
        cur.next = node;

    }
    //任意位置插入,第一个数据节点为0号下标
   public void addIndex(int index,int data){
        int len =size();
        if (index<0||index>len){
            System.out.println("位置不合法");
        }
        if (index==0){
            addFist(data);
            return;
        }
        if(index==len){
            addLast(data);
            return;
        }
        //中间插入
       ListNode cur =head;
        while (index-1 !=0){
            cur = cur.next;
            index--;
        }
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
   }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = findVal(key);
        if (head==null){
            return;
    }
        if (head.val==key){
            head=head.next;
            return;
        }
        if (cur==null){
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }

    private ListNode findVal(int key) {
        ListNode cur =head;
        while ( cur.next !=null) {
            if (cur.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if (head==null){
            return;
        }
        ListNode prev = head;
        ListNode cur  = head.next;
        while (cur !=null){
            if (cur.val==key){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev = cur;
                cur =cur.next;
            }
        }
        if (head.val==key){
            head = cur.next;
        }
    }
    //清除所有链表的值
    public void clear() {
        ListNode cur = head;
        while (cur !=null){
            ListNode curN = cur.next;
           cur.next=null;
           cur = curN;
        }
    }
}
